
#include "CMenuScreen.h"
#include "CTestScreen.h"
#include "CHelpScreen.h"
#include "CSettingsScreen.h"

void CMenuScreen::load( CGraphics *gl )
{
    selection = 0;
    loadingTo = -1;
    font = QFont("Verdana", 15, 0, 0);

    labels.append( TMLabel(QString::fromStdWString(L"Biliardas"), 300, 120, font, -1 ) );
    buttons.append( TMLabel(QString::fromStdWString(L"\"8 Pool\""), 300, 150, font, 0 ) );
    buttons.append( TMLabel(QString::fromStdWString(L"\"9 Pool\""), 300, 170, font, 1 ) );
    buttons.append( TMLabel(QString::fromStdWString(L"\"Quick pool\""), 300, 190, font, 2) );
    buttons.append( TMLabel(QString::fromStdWString(L"Nustatymai"), 300, 210, font, 3) );
    buttons.append( TMLabel(QString::fromStdWString(L"Pagalba"), 300, 230, font, 4) );
    buttons.append( TMLabel(QString::fromStdWString(L"Baigti"), 300, 250, font, 5) );
}

void CMenuScreen::unload( CGraphics *gl )
{

}

int CMenuScreen::update( CInput *in )
{
    int action = -1;

    if ( loadingTo != -1 ) {
        if ( loadingTo == 0 ) {
            sm->removeScreen("backgroundScreen");
            CTestScreen* ts = new CTestScreen("testScreen");
            sm->addScreen( ts );
            ts->setGameSettings( 0, 0 );
            sm->removeScreen(this);
            return 0;
        } else if ( loadingTo == 1 ) {
            sm->removeScreen("backgroundScreen");
            CTestScreen* ts = new CTestScreen("testScreen");
            sm->addScreen( ts );
            ts->setGameSettings( 1, 0 );
            sm->removeScreen(this);
            return 0;
        } else if ( loadingTo == 2 ) {
            sm->removeScreen("backgroundScreen");
            CTestScreen* ts = new CTestScreen("testScreen");
            sm->addScreen( ts );
            ts->setGameSettings( 2, 0 );
            sm->removeScreen(this);
            return 0;
        }
    }

    if ( in->wasKeyPressed(Qt::Key_Return) )    // Enter
        action = selection;

    if ( in->wasButtonPressed(Qt::LeftButton) )    // pelės paspaudimas
        foreach ( TMLabel l, buttons ) {
            int Mx = in->getCursorPos().x();
            int My = in->getCursorPos().y();
            if ( l.x < Mx && Mx < l.x2 && l.y2+10 < My && My < l.y+10 ) {
                action = l.action;
            }
        }

    if ( in->hasMouseMoved() )           // pelės žymėjimas
        foreach ( TMLabel l, buttons ) {
            int Mx = in->getCursorPos().x();
            int My = in->getCursorPos().y();
            if ( l.x < Mx && Mx < l.x2 && l.y2+10 < My && My < l.y+10 ) {
                if ( selection != l.action )
                    sm->getSound()->play( "audio/opr000L0-6.mp3", 0.5 );
                selection = l.action;
            }
        }

    if( ( in->wasKeyPressed(Qt::Key_Down) || in->wasKeyPressed(Qt::Key_Right) )
        && selection < buttons.size()-1 ) {  //  žymėjimas klaviatūra
        selection++;
        sm->getSound()->play( "audio/opr000L0-6.mp3", 0.5 );
    } else if( ( in->wasKeyPressed(Qt::Key_Up) || in->wasKeyPressed(Qt::Key_Left) )
        && selection > 0 ) {
        selection--;
        sm->getSound()->play( "audio/opr000L0-6.mp3", 0.5 );
    }

    if ( in->wasKeyPressed(Qt::Key_Escape) )
        action = 5; // Baigti žaidimą

    if ( action == 0 ) {    // 8 pool
        loadingTo = 0;
    } else if ( action == 1 ) { // 9 pool
        loadingTo = 1;
    } else if ( action == 2 ) { // quick fire pool
        loadingTo = 2;
    } else if ( action == 3 ) { // Nustatymai
        sm->addScreen( new CSettingsScreen("settingsScreen") );
        this->setHidden( true );
//        QMessageBox::information( 0, QString::fromStdWString(L"Dėja..."),
//                                  QString::fromStdWString(L"Ši žaidimo funkcija dar nėra sukurta") );
    } else if ( action == 4 ) { // pagalba
        sm->addScreen( new CHelpScreen("helpScreen") );
        this->setHidden( true );
    } else if ( action == 5 ) { // Baigti
        return -1;
    }

    for ( int i = 0; i < buttons.size(); i++ ) {
        if ( i == selection ) {
            buttons[i].fade += 0.2;
            if ( buttons[i].fade > 2.0 ) buttons[i].fade = 2.0;
        } else {
            if ( buttons[i].fade > 0.0 ) {
                buttons[i].fade -= 0.1;
                if ( buttons[i].fade < 0.0 ) buttons[i].fade = 0.0;
            }
        }
    }

    return 0;
}

void CMenuScreen::draw(CGraphics *gl)
{
    glLoadIdentity();

    glColor4f( 1.0f, 1.0f, 1.0f, 1.0f );
    foreach ( TMLabel l, labels ) {
        gl->renderTexts( l.x, l.y, l.text, font );
    }
    foreach ( TMLabel l, buttons ) {
//        if ( selection == l.action ) glColor3f( 0.8f, 0.8f, 0.8f ); else glColor3f(1.0f, 1.0f, 1.0f );
        glColor4f( 1.0f, 1.0f, 1.0f, 0.5f+0.25*l.fade );
        gl->renderTexts( l.x, l.y, l.text, font );
    }

    if ( loadingTo != -1 ) {
        glDisable(GL_TEXTURE_2D);       // piešiamas pusiau permatomas juodas ekranas
        glColor4f(0.0f, 0.0f, 0.0f, 0.8f );
        glBegin(GL_QUADS);  // fonas
            glVertex2i(  0, 0 );
            glVertex2i(  0, 500 );
            glVertex2i(700, 500 );
            glVertex2i(700, 0 );
        glEnd();
        glEnable(GL_TEXTURE_2D);

        glColor4f(1.0f, 1.0f, 1.0f, 1.0f );
        gl->renderTexts( 300, 250, QString("Kraunasi..."), font );
    }

}
